package org.scalaconsole.ui

import java.io.File

import javax.swing.{SwingUtilities, UIManager}

object Main {
  def main(args : Array[String]) : Unit = {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName)
    }
    catch {
      case _ => //ignore
    }

    val scalaConsole = new ScalaConsole
    findScala() match {
      case Some((lp, cp)) => {
        scalaConsole.libraryPath = Some(lp)
        scalaConsole.compilerPath = Some(cp)
      }
      case None => // leave values as they are
    }

    SwingUtilities.invokeLater(scalaConsole)
  }

  /**
   * Locates the scala-library jar and the scala-compiler jar.
   *
   * @return an {@code Option} tuple containing the path to scala-library.jar and scala-compiler.jar respectively
   */
  def findScala(): Option[(String, String)] = {
    System getenv "SCALA_HOME" match {
      case null => None
      case "" => None
      case path: String => {
        if (!new File(path).exists) {
          None
        }
        val prefix = withSeparator(path) + "bin" + File.separator
        Some((prefix + ScalaConsole.libraryJarName, prefix + ScalaConsole.compilerJarName))
      }
    }
  }

  def withSeparator(path: String) = {
    if (path endsWith File.separator)
      path
    else
      path + File.separator
  }
}
